package ypmits.physics {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author Dave Lenz
	 * 
	 * @Extend this class and use the start/stop to set it in motion
	 */
	public class BrownianMotion extends Sprite {

		protected var vx : Number = 0;
		protected var vy : Number = 0;
		protected var vz : Number = 0;
		protected var dampen : Number = 0.94;

		public function BrownianMotion( obj : DisplayObject = null ) {
			if( obj ) {
				addChild( obj );
			}
			vx += Math.random( ) * 2.4;
			vy -= Math.random( ) * 2.4;
			vz += Math.random( ) * 0.002 - 0.001;
		}
		
		
		public function setVariables( velocityX : Number = 2.4, velocityY : Number = 2.4, velocityZ : Number = 0.001 ) : void {
			vx += Math.random() * velocityX;
			vy += Math.random() * velocityY;
			vz += Math.random() * velocityZ;
		}

		
		public function start() : void {
			addEventListener(Event.ENTER_FRAME, enter );
		}

		
		public function stop() : void {
			removeEventListener(Event.ENTER_FRAME, enter );
		}
		
		
		protected function enter(event : Event) : void {
			x += vx;
			y += vy;
			
			vx *= dampen;
			vy *= dampen;
			vz *= dampen;
		}
	}
}
